package it.unibo.util.information.test.unit;

import static org.junit.Assert.*;
import it.unibo.util.information.Information;
import it.unibo.util.information.InformationConverter;
import it.unibo.util.information.PrologInformationConverter;

import org.junit.BeforeClass;
import org.junit.Test;

public class PrologInformationConverterTestCase 
{
	InformationConverter converter = new PrologInformationConverter();

	static Information emptyInfo;
	static String emptyInfoSTR = "info([])";
	static Information simpleInfo;
	static String simpleInfoSTR = "info([],att(val))";
	static Information complexInfo;
	static String complexInfoSTR = "boatInfo([myInfo([],att(val))],speed(45),longitude(76.54),latitude(45.66))";
	static Information superComplexInfo;
	static String superComplexInfoSTR = "info([info2([info3([info4([])])])])";

	static String wrongInfoSTR = "info([], att(val)";

	@BeforeClass
	public static void setUp()
	{
		emptyInfo = new Information("info");

		simpleInfo = new Information("info");
		simpleInfo.addAttribute("att", "val");

		complexInfo = new Information("boatInfo");
		complexInfo.addAttribute("speed", "45");
		complexInfo.addAttribute("longitude", "76.54");
		complexInfo.addAttribute("latitude", "45.66");
		Information myInfo = new Information("myInfo");
		myInfo.addAttribute("att", "val");
		complexInfo.addChildInformation(myInfo);

		superComplexInfo = new Information("info");
		Information info2 = new Information("info2");
		Information info3 = new Information("info3");
		Information info4 = new Information("info4");
		superComplexInfo.addChildInformation(info2);
		info2.addChildInformation(info3);
		info3.addChildInformation(info4);
	}

	@Test(expected=IllegalArgumentException.class)
	public void testCreateInformationEmptyString() 
	{
		//Wrong information 
		new Information("");
	}

	@Test(expected=IllegalArgumentException.class)
	public void testCreateInformationNullString() 
	{
		//Wrong information 
		new Information(null);
	}

	@Test
	public void testToInformation() 
	{
		// Empty Information
		assertEquals(emptyInfo, converter.toInformation(emptyInfoSTR));

		// Simple Information
		assertEquals(simpleInfo, converter.toInformation(simpleInfoSTR));

		// Complex Information
		assertEquals(complexInfo, converter.toInformation(complexInfoSTR));

		// "Super" Complex Information
		assertEquals(superComplexInfo, converter.toInformation(superComplexInfoSTR));

		assertNotSame(superComplexInfo, converter.toInformation(complexInfoSTR));
	}

	@Test(expected=IllegalArgumentException.class)
	public void testWrongInfo()
	{
		converter.toInformation(wrongInfoSTR);
	}

	@Test
	public void testToString() 
	{
		// Empty Information
		assertEquals(emptyInfoSTR, converter.toString(emptyInfo));

		// Simple Information
		assertEquals(simpleInfoSTR, converter.toString(simpleInfo));

		// Complex Information
		assertEquals(complexInfoSTR, converter.toString(complexInfo));

		// "Super" Complex Information
		assertEquals(superComplexInfoSTR, converter.toString(superComplexInfo));

		assertNotSame(complexInfoSTR, converter.toString(superComplexInfo));
	}
}
